<?php
/**
*	@author SignpostMarv Martin
*	@package UHU
*	@subpackage HTTP
*/

/**
*	@package UHU
*	@subpackage HTTP
*	@link http://hoohoo.ncsa.uiuc.edu/cgi/out.html
*	@uses PHP_SAPI
**/
class uhu_headerBySAPI
{
/**
*	@var bool|null TRUE if PHP is running under CGI, FALSE otherwise
*/
	private static $is_cgi = null;

/**
*	string the string to look for in PHP_SAPI
*/
	const string_cgi = 'cgi';
/**
*	string The string to replace the HTTP/*.* portion of a header
*/
	const string_status = 'Status:';
/**
*	string The error message thrown
*/
	const error_no_arguments = 'No Arguments Passed';
/**
*	callback the function to call to execute the header
*/
	const func_header = 'header';
/**
*	@uses uhu_headerBySAPI::$is_cgi
*	@uses uhu_headerBySAPI::string_cgi
*/
	final private function __construct()
	{
		uhu_headerBySAPI::$is_cgi = (substr(PHP_SAPI,0,3) === uhu_headerBySAPI::string_cgi);
	}
/**
*	Makes a HTTP/*.* type header usable under a {@link http://hoohoo.ncsa.uiuc.edu/cgi/out.html CGI environment}
*	@link http://uk.php.net/header
*	@uses uhu_headerBySAPI::$is_cgi
*	@uses uhu_exception
*	@uses uhu_headerBySAPI::error_no_arguments
*	@uses uhu_headerBySAPI::string_status
*	@uses uhu_headerBySAPI::func_header
*/
	final public static function headerBySAPI()
	{
		if(isset(self::$is_cgi) === false)
		{
			new self;
		}
		$num_args = func_num_args();
		if($num_args === 0)
		{
			throw new uhu_exception(self::error_no_arguments);
		}
		$args = func_get_args();
		if(self::$is_cgi)
		{
			$header = & $args[0];
			if(substr($header,0,5) === 'HTTP/')
			{
				$header = substr_replace($header,self::string_status,0,8);
			}
		}
		return call_user_func_array(self::func_header,$args);
	}
}
?>